package com.example.controller;

import com.example.po.Sys_User;
import com.example.service.SysUserService;
import com.example.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @auter czcjq
 * @date 2022/5/16 17:38
 */
@Controller
@RequestMapping("/user")
public class Sys_UserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户登录
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public UserVo login(String tel, String password, HttpSession session) {
        Sys_User user = sysUserService.login(tel, password);
        UserVo userVo = new UserVo();
     /*   System.out.println(user);*/
        if (user != null) {
            userVo.setCode(1);
            userVo.setMsg("登陆成功！");
            session.setAttribute("SysUser",user);
        } else {
            userVo.setCode(0);
            userVo.setMsg("账户名或者密码错误");
        }
        return userVo;
    }

    /**
     * 注册
     */
    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public UserVo register(Sys_User user, HttpSession session) {
        UserVo userVo = new UserVo();
        int i = sysUserService.register(user);
        if (i != 0) {
            userVo.setCode(1);
            userVo.setMsg("注册成功！");
        }
        System.out.println(i);

        return userVo;

    }
    //用户是否登录验证
    @ResponseBody
    @RequestMapping(value = "/dengludo",method = RequestMethod.GET)
    public UserVo ifLogIn(HttpSession session){
        Sys_User user = (Sys_User) session.getAttribute("SysUser");

        UserVo ar=new UserVo();
        int id=0;
        if(user!=null) {
            ar.setCode(1);
            ar.setUserLogged(user);
        } else{
            ar.setCode(0);
        }
        /*System.out.println("登陆过"+ar);*/
        return ar;
    }

    //修改用户信息
    @ResponseBody
    @RequestMapping(value ="/updateinfo", method = RequestMethod.GET)
    public UserVo update(Sys_User user) {

        UserVo uo = new UserVo();
        boolean b = sysUserService.updateOne(user);
        if (b) {
            uo.setCode(1);
            uo.setMsg("修改成功");
        }
        System.out.println("修改用户"+uo);
        return uo;
    }


    @ResponseBody
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public UserVo update(HttpSession session) {
        session.invalidate();
        UserVo uo = new UserVo();
        uo.setCode(1);
        uo.setMsg("退出成功");
        return uo;
    }


}
